﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace NczControlLibrary.Controls {
    [TemplatePart(Name="fPath",Type=typeof(System.Windows.Shapes.Path))]
    public class IndicatorButton : Control {
        enum Vsn {
            normal, down, up
        }
        public IndicatorButton() {
            base.DefaultStyleKey = typeof(IndicatorButton);
            this.MouseLeftButtonDown += new MouseButtonEventHandler(IndicatorButton_MouseLeftButtonDown);
            this.MouseLeftButtonUp += new MouseButtonEventHandler(IndicatorButton_MouseLeftButtonUp);
            this.MouseLeave += new MouseEventHandler(IndicatorButton_MouseLeave);
            this.BorderBrush = new SolidColorBrush(Colors.Gray);
            this.Foreground = new SolidColorBrush(Colors.Black);
            this.PathFill = new SolidColorBrush(Colors.Black);
            this.Background = new SolidColorBrush(Colors.Transparent);
        }

        void IndicatorButton_MouseLeave(object sender,MouseEventArgs e) {
            VisualStateManager.GoToState(this,Vsn.normal.ToString(),false);
        }
        System.Windows.Shapes.Path path;
        public override void OnApplyTemplate() {
            this.path = this.GetTemplateChild("fPath") as System.Windows.Shapes.Path;
        }
        public event EventHandler eClick;
        void OneClick() {
            if (this.eClick != null) {
                this.eClick.Invoke(this,EventArgs.Empty);
            }
        }
        void IndicatorButton_MouseLeftButtonUp(object sender,MouseButtonEventArgs e) {
            this.OneClick();
            VisualStateManager.GoToState(this,Vsn.up.ToString(),false);
        }

        void IndicatorButton_MouseLeftButtonDown(object sender,MouseButtonEventArgs e) {
            this.IsSelected = true;
            VisualStateManager.GoToState(this,Vsn.down.ToString(),false);
        }
        #region dependency properties
        public static readonly DependencyProperty StrokeThicknessProperty = DependencyProperty.Register("StrokeThickness",typeof(double),typeof(IndicatorButton),new PropertyMetadata(0.1d,
(o,k) => { }));
        public double StrokeThickness { get { return (double)this.GetValue(StrokeThicknessProperty); } set { this.SetValue(StrokeThicknessProperty,value); } }
        public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register("Stroke",typeof(Brush),typeof(IndicatorButton),new PropertyMetadata(new SolidColorBrush(Colors.Gray),
(o,k) => { }));
        public Brush Stroke { get { return (Brush)this.GetValue(StrokeProperty); } set { this.SetValue(StrokeProperty,value); } }
        public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected",typeof(bool),typeof(IndicatorButton),new PropertyMetadata(
(o,k) => { var sender = o as IndicatorButton; if ((bool)k.NewValue && sender.path != null) sender.path.Visibility = Visibility.Visible; else if (sender.path != null)sender.path.Visibility = Visibility.Collapsed; }));
        public bool IsSelected { get { return (bool)this.GetValue(IsSelectedProperty); } set { this.SetValue(IsSelectedProperty,value); } }
        public static readonly DependencyProperty IntervalProperty = DependencyProperty.Register("Interval",typeof(double),typeof(IndicatorButton),new PropertyMetadata(
(o,k) => {
    var sender = o as IndicatorButton; var v = (double)k.NewValue >= 20 ? 20 : (double)k.NewValue; sender.SetValue(IntervalProperty,v);
}));
        public double Interval { get { return (double)this.GetValue(IntervalProperty); } set { var v = value >= 20 ? 20 : value; this.SetValue(IntervalProperty,v); } }
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text",typeof(string),typeof(IndicatorButton),new PropertyMetadata(
(o,k) => {
    var sender = o as IndicatorButton; var vv = (string)k.NewValue; var v = vv.Split(new[] { ',' },StringSplitOptions.RemoveEmptyEntries);
    if (v.Length <= 1) { sender.SetValue(TextProperty,vv); } else if (v.Length == 2) { sender.SetValue(TextProperty,string.Format("{0}({1})",v[0],v[1])); } else { sender.SetValue(TextProperty,v[0]); }
}));
        public string Text {
            get { return (string)this.GetValue(TextProperty); }
            set {
                var v = value.Split(new[] { ',' },StringSplitOptions.RemoveEmptyEntries);
                if (v.Length <= 1) { this.SetValue(TextProperty,value); } else if (v.Length == 2) { this.SetValue(TextProperty,string.Format("{0}({1})",v[0],v[1])); } else { this.SetValue(TextProperty,v[0]); }
            }
        }
        public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius",typeof(CornerRadius),typeof(IndicatorButton),new PropertyMetadata(
(o,k) => { }));
        public CornerRadius CornerRadius { get { return (CornerRadius)this.GetValue(CornerRadiusProperty); } set { this.SetValue(CornerRadiusProperty,value); } }
        public static readonly DependencyProperty PathFillProperty = DependencyProperty.Register("PathFill",typeof(SolidColorBrush),typeof(IndicatorButton),new PropertyMetadata(
(o,k) => { }));
        public SolidColorBrush PathFill { get { return (SolidColorBrush)this.GetValue(PathFillProperty); } set { this.SetValue(PathFillProperty,value); } }
        #endregion
    }
    public class IndicatorVisibility : IValueConverter {

        #region IValueConverter 成员

        public object Convert(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture) {
            var v = (bool)value;
            var r = System.Windows.Visibility.Collapsed;
            r = v ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            return r;
        }

        public object ConvertBack(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture) {
            throw new NotImplementedException();
        }

        #endregion
    }
}
